#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
char path[100]; 
VOID ManagerRun(LPCSTR exe,LPCSTR param,INT nShow=SW_SHOW)  
{
 SHELLEXECUTEINFO ShExecInfo; 
 ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);  
 ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;  
 ShExecInfo.hwnd = NULL;  
 ShExecInfo.lpVerb = "runas";  
 ShExecInfo.lpFile = exe; 
 ShExecInfo.lpParameters = param;   
 ShExecInfo.lpDirectory = NULL;  
 ShExecInfo.nShow = nShow;  
 ShExecInfo.hInstApp = NULL;   
 BOOL ret = ShellExecuteEx(&ShExecInfo);  
 CloseHandle(ShExecInfo.hProcess);
 return;
}

void color(const unsigned short color1)
{        /*仅限改变0-15的颜色;如果在0-15那么实现他的颜色   因为如果超过15后面的改变的是文本背景色。*/
        if(color1>=0&&color1<=15)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color1);
        /*如果不在0-15的范围颜色，那么改为默认的颜色白色；*/
    else
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}


void get_path () 
{
	system("mkdir c:\\Tetsudo");
	system("for /f \"tokens=1,2,* \" %i in (\'REG QUERY \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Huorong\\Sysdiag\" ^| find /i \"InstallPath\ \"\') do echo %k >C:\\tetsudo\\temp.info");
	FILE *tempfile;
	tempfile=fopen("C:\\tetsudo\\temp.info", "r+");
	if(tempfile == NULL)
	{
		color(12); 
        printf("[×]无法打开文件！");
        system("pause"); 
        color(16);
        system("cls"); 
        exit(-1);
	}
	while(!feof(tempfile))
		fscanf(tempfile,"%s",path);
	fclose(tempfile);	
	system("del /q /f C:\\tetsudo\\temp.info");
	} 
int main(int argc,char *argv[])
{
    if(argc == 1) 
    {
       ShowWindow(GetConsoleWindow(),SW_HIDE);
       ManagerRun(argv[0],"2");
       return 1;
    }else if(argc == 2) 
    {
	int option;
	int create_task();
	int modify_task();
	int delete_task();
	system("title 火绒定时查杀工具");
	start:
	printf("Huorong Timed Scanning Tool [版本 1.0.0.0 Inside Preview] \n");
	printf("(c) Tetsudo Software。 保留所有权利。\n");
	color(10);
	printf("[支  持]1.创建新计划任务 \n");
	color(6);
	printf("[不支持]2.修改已创建计划 \n");
	color(6);
	printf("[不支持]3.删除计划任务 \n");
	color(16);
	//printf("4. 检查更新\n");
	printf("请在下面输入对应功能的数字：\n");
	scanf("%d",&option);
	
	if(option == 1)
		create_task();
	if(option == 2)
		modify_task();
	if(option == 3)
		delete_task();
	system("cls");
	goto start;
	}
} 
int create_task ()
{
	get_path();
	char frequency[20],name[30],time[6],drive[2];int option,status;//函数变量定义区
	char command[500]="schtasks /create /tn ";
	char command2[500]="echo ";
	system("cls");
	printf("请输入任务名称：\n");
	color(6);
	printf("[!]警告：暂不支持中文名称\n");
	color(16);
	scanf("%s",name);
	system("cls");
	printf("1.每天\n"); 
	printf("2.每周\n");
	printf("3.每月\n");
	printf("4.系统启动后就运行\n");
	printf("请输入选项：");
	scanf("%d",&option);
	if(option == 1)
	strcpy(frequency, "Daily");
	else if(option == 2)
	strcpy(frequency, "Weekly");
	else if(option == 3)
	strcpy(frequency, "MONTHLY");
	else if(option == 4)
	strcpy(frequency, "ONSTART");
	else printf("No Such Value");
	system("cls");
	printf("请输入项目的启动时间：(例如：14:20,为了保证任务不冲突，建议将时间隔得开一点):");
	scanf("%s",time);
	system("cls");
	printf("请输入需要扫描的盘符：如C:");
	scanf("%s",drive);
	system("cls");
	//开始拼接字符串 
	strcat(command,name); 
	strcat(command," /sc ");
	strcat(command,frequency);
	strcat(command," /st ");
	strcat(command,time);
	strcat(command," /tr \"");
	strcat(command,path);
	strcat(command,"\\bin\\HipsMain.exe -s");
	strcat(command,drive);
	strcat(command,": \"");
	/*拼接另外的字符串*/
	strcat(command2,name);
	strcat(command2," >> C:\\Tetsudo\\log.db");
	color(11);
	system(command);
	system(command2);
	color(16);
	system("pause"); 
}
int modify_task ()
{
	color(12);
	printf("[×]错误：暂不支持!\n");
	color(16);
	system("pause");
	system("cls"); 
}
int delete_task ()
{
	color(12);
	printf("[×]错误：暂不支持!\n");
	color(16);
	system("pause");
	system("cls"); color(12);
	printf("[×]错误：暂不支持!\n");
	color(16);
	system("pause");
	system("cls"); 

}
